#include "InGameMenu.h"

// Initialize the instance to null
InGameMenu* InGameMenu::m_InGameMenuInstance = 0;

InGameMenu::InGameMenu(void)
{
}

InGameMenu::~InGameMenu(void)
{
}

InGameMenu* InGameMenu::GetInstance(void)
{
	// Create the singleton if it doesn't exist
	if(!m_InGameMenuInstance)
		m_InGameMenuInstance = new InGameMenu;

	// Return the singleton
	return m_InGameMenuInstance;
}

void InGameMenu::DeleteInGameMenuInstance(void)
{
	// If instance exist, delete it!
	if(m_InGameMenuInstance)
	{
		delete m_InGameMenuInstance;
		m_InGameMenuInstance = NULL;
	}
}

void InGameMenu::InitInGameMenu(IDirect3DDevice9* m_pD3DDevice)
{
	// Get the instance and initialize the sprite manager
	m_pSpriteManager = SpriteManager::GetInstance();

	// Initialize sound variable to default constructor
	m_pSound = Sound::GetSoundInstance();

	//return sprite index number
	sSpriteIndex = m_pSpriteManager->GetSpriteCount();
}

void InGameMenu::LoadPauseMenu(void)
{
	// Initialize the boolean to draw the main menu
	sMenuState = PAUSED;
	iGameState = 1;
	bPaused = true;

	// In Game Menu Sprites
	sResumeGame = m_pSpriteManager->LoadSprite("InGameMenuSprites\\inGameResume.png", D3DXVECTOR3(470, 350, 0), 0.5f);
	sInGameInstructions = m_pSpriteManager->LoadSprite("InGameMenuSprites\\inGameInstructions.png", D3DXVECTOR3(510, 400, 0), 0.7f);
	sResetGame = m_pSpriteManager->LoadSprite("InGameMenuSprites\\inGameReset.png", D3DXVECTOR3(450, 450, 0), 0.5f);
	sQuitToMainMenu = m_pSpriteManager->LoadSprite("InGameMenuSprites\\quitToMainMenu.png", D3DXVECTOR3(475, 500, 0), 0.6f);

	// Instruction screen sprite
	sInstructionScreen = m_pSpriteManager->LoadSprite("MenuSprites\\Instructions Sprites\\controlsInstructions.png", D3DXVECTOR3(512, 284, 0), 0.8f);

	// Back Button Sprite
	sBackButton = m_pSpriteManager->LoadSprite("MenuSprites\\backButton.png", D3DXVECTOR3(512, 700, 0), 0.5f);
}

void InGameMenu::Update(HWND hWnd, DirectInput* m_pDirectInput)
{
	// Update audio
	m_pSound->Update();

	// Menu choices
	m_pDirectInput->Update(hWnd);
	if(m_pDirectInput->CheckMouseButtons(0))
	{
		// RESUME
		if(m_pSpriteManager->SpriteCollision(sResumeGame, m_pDirectInput->currentMousePos))
		{
			m_pSound->MenuButtonClickSFX();
			bPaused = false;
		}

		// INSTRUCTIONS
		if(m_pSpriteManager->SpriteCollision(sInGameInstructions, m_pDirectInput->currentMousePos))
		{
			m_pSound->MenuButtonClickSFX();
			sMenuState = INSTRUCTIONS;
		}

		// QUIT TO MAIN MENU
		if(m_pSpriteManager->SpriteCollision(sQuitToMainMenu, m_pDirectInput->currentMousePos))
		{
			m_pSound->MenuButtonClickSFX();
			iGameState = 0;
		}

		// BACK BUTTON
		if(m_pSpriteManager->SpriteCollision(sBackButton, m_pDirectInput->currentMousePos))
		{
			m_pSound->MenuButtonClickSFX();
			sMenuState = PAUSED;
		}

		// RESET LEVEL
		if(m_pSpriteManager->SpriteCollision(sResetGame, m_pDirectInput->currentMousePos))
		{
			m_pSound->MenuButtonClickSFX();
			iGameState = 4; // 4 is unused so we can check it to know if we have to call the reset function
		}
	}
}

void InGameMenu::Render(IDirect3DDevice9* m_pD3DDevice)
{	
	// Menu state switch
	switch(sMenuState)
	{
	case PAUSED:
		// Draw in game menu sprites if in state 0: in game menu
		for(int i = sResumeGame; i <= sQuitToMainMenu; i++)
			m_pSpriteManager->DrawSprite(i);
		break;
	case INSTRUCTIONS:
		// Render the instruction sprites if in state 1 
		m_pSpriteManager->DrawSprite(sInstructionScreen);
		m_pSpriteManager->DrawSprite(sBackButton);
		break;
	}
}

void InGameMenu::ResetInGameMenu(void)
{
	// Reset the sMenuState so that if you escape at the instruction screen you will not go 
	// back to the instruction screen when you pause again.
	sMenuState = PAUSED; 
	iGameState = 1;
	bPaused = true;
}

int InGameMenu::GetStateIGMenu(void)
{
	// Return the game state
	return iGameState;
}

bool InGameMenu::GetPaused(void)
{
	// Is game paused?
	return bPaused;
}

void InGameMenu::DestroyIGMenu(void)
{
	for(int i = sBackButton; i >= sResumeGame; i--)
		m_pSpriteManager->DestroySprite(i);
}